package cc.rengu.igas.channel.alipay.core.service.trans;

import cc.rengu.igas.channel.alipay.common.constant.AlipayParamConstant;
import cc.rengu.igas.channel.alipay.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.alipay.core.model.request.AlipayResultNoticeRequest;
import cc.rengu.igas.channel.alipay.core.model.response.AlipayResultNoticeResponse;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.FinanceTxnMapper;
import cc.rengu.oltp.service.common.dao.impl.FinanceTxnMapperImpl;
import cc.rengu.oltp.service.common.entity.FinanceTxn;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝支付结果通知
 * Created by 王成 on 2018/6/1.
 */
public class AlipayResultNoticeService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        AlipayResultNoticeRequest alipayResultNoticeRequest = new AlipayResultNoticeRequest();
        ConvertUtil.convertOutput(alipayResultNoticeRequest);
        return alipayResultNoticeRequest;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse bizResponse = new BizResponse();
        AlipayResultNoticeResponse alipayResultNoticeResponse = new AlipayResultNoticeResponse();
        AlipayResultNoticeRequest alipayResultNoticeRequest = (AlipayResultNoticeRequest) request;
        if (null == alipayResultNoticeRequest.getOutTradeNo() || alipayResultNoticeRequest.getOutTradeNo().isEmpty() ||
                null == alipayResultNoticeRequest.getTradeStatus() || alipayResultNoticeRequest.getTradeStatus().isEmpty()) {
            rglog.error("out_trade_no or trade_status,不存在:error ");
            throw new BizException(RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespCode(), RespCodeEnum.ALIP_NOTICE_TXN_ERROR.getRespDesc());
        }
        //还原交易码
        /* 查找BIZ_PROCESS的注解，基于注解实现不同场景的业务逻辑 */
        invokeBizAnnoMethod(BIZ_PROCESS.class);

        alipayResultNoticeResponse.setAliRetCode("success");
        bizResponse.setRspSysId(AlipayParamConstant.TUNL_ID_ALIP);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(alipayResultNoticeResponse);
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        BizResponse bizResponse = response;
        AlipayResultNoticeResponse alipayResultNoticeResponse = (AlipayResultNoticeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(alipayResultNoticeResponse);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
